﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;

namespace Caching
{
    public static class ZCache
    {
        private static Cache GetCache()
        {
            HttpContext cntx = HttpContext.Current;
            return cntx != null ? cntx.Cache : HttpRuntime.Cache;
        }

        public static void InsertCache(string key, object obj, int seconds, CacheDependency dep)
        {
            key = key.ToLower();
            if (obj != null)
                GetCache().Insert(key, obj, dep, DateTime.Now.AddSeconds(seconds), TimeSpan.Zero,
                                  CacheItemPriority.Normal, null);
        }

        public static void InsertCache(string key, object obj, int seconds)
        {
            key = key.ToLower();
            if (obj != null)
                GetCache().Insert(key, obj, null, DateTime.Now.AddSeconds(seconds), TimeSpan.Zero,
                                  CacheItemPriority.Normal, null);
        }

        public static void MaxCache(string key, object obj)
        {
            MaxCache(key, obj, null);
        }

        public static void MaxCache(string key, object obj, CacheDependency dep)
        {
            GetCache().Insert(key, obj, dep, DateTime.Now.AddDays(1), TimeSpan.Zero, CacheItemPriority.NotRemovable,
                              null);
        }

        public static T Get<T>(string key) where T : class
        {
            key = key.ToLower();
            return GetCache()[key] as T;
        }

        public static int GetInt(string key)
        {
            key = key.ToLower();
            object obj = GetCache()[key];
            int result;
            if (obj != null && int.TryParse(obj.ToString(), out result))
                return result;
            return -1;
        }

        public static void RemoveCache(string key)
        {
            key = key.ToLower();
            GetCache().Remove(key);
        }

        public static void Clear()
        {
            Cache cache = GetCache();
            IDictionaryEnumerator cacheEnum = cache.GetEnumerator();
            var keys = new List<string>();
            while (cacheEnum.MoveNext())
            {
                keys.Add(cacheEnum.Key.ToString());
            }

            foreach (string key in keys)
            {
                cache.Remove(key);
            }
        }

        public static void RemoveByPattern(string pattern)
        {
            Cache cache = GetCache();
            IDictionaryEnumerator cacheEnum = cache.GetEnumerator();
            var regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
            var keys = new List<string>();
            while (cacheEnum.MoveNext())
            {
                if (regex.IsMatch(cacheEnum.Key.ToString()))
                    keys.Add(cacheEnum.Key.ToString());
            }

            if (keys.Count <= 0) return;
            foreach (string key in keys)
            {
                cache.Remove(key);
            }
        }
    }
}